const koa = require("koa");
const app = new koa;
const koaBody = require("koa-body");
const router = require("koa-router")();
const cors = require("koa2-cors")

router.post("/login",async ctx=>{
    console.log(ctx.request.body);
    var {username,pass} = ctx.request.body;
    if(username == "cheng"&&pass=="123456"){
        ctx.cookies.set("loginAuth",true,{
            httpOnly:false
        })
        ctx.body = {
            code:200,
            msg:"登录成功"
        }
    }else{
        ctx.body = {
            code:400,
            msg:"登录失败，用户名或密码错误"
        }
    }
})

/* 后端配置cookie可以实现跨域访问 */
app.use(cors({
    origin:ctx=>{
        return ctx.headers.origin;
    },
    credentials:true
}))
app.use(koaBody());
app.use(router.routes());
app.listen(8000)
